package com.blast.comicsisland.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.blast.comicsisland.R;
import com.blast.comicsisland.model.community.Info;
import com.blast.comicsisland.model.community.Picurl;
import com.blast.comicsisland.model.myview.PuBuLu;
import com.blast.comicsisland.utils.ImageLoaderUtil;
import com.blast.comicsisland.utils.JsonUtils;
import com.blast.comicsisland.utils.XUtilsImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * 圈子Fragment 上方为四个Button 壁纸 涂鸦 讨论 书评（可忽略）
 * 下方为瀑布流 网络加载 此版本只提供展示 不考虑点击事件
 */
public class CommunityFragment extends Fragment implements PuBuLu.OnLoadMoreListener, PuBuLu.OnItemOnClick {

    private PuBuLu puBuLu;
    private View view;
    private int pageno = 1;
    private List<Info> infos;
    private ImageLoader imageLoader;
    private XUtilsImageLoader xUtilsImageLoader;

    public CommunityFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImageLoaderUtil imageLoaderUtil = ImageLoaderUtil.getInstance();
        imageLoader = imageLoaderUtil.getImageLoader();
        xUtilsImageLoader = new XUtilsImageLoader(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_community, container, false);
        puBuLu = (PuBuLu) view.findViewById(R.id.community_pubulu);
        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.community_scrollview_head, null);
        puBuLu.addHeadView(headView);

        puBuLu.setOnLoadMoreListener(this);
        puBuLu.setOnClickItem(this);

        String path = "http://mhjk.1391.com/comic/getcommunitylist?e=" + (System.currentTimeMillis() / 1000);

        String jsonPath = "{\"userid\":\"\",\"pagesize\":\"10\",\"sort\":\"5\",\"isgood\":\"1\",\"pageno\":" + pageno + ",\"communityid\":\"0\"}";

        CommunityAsyncTask communityAsyncTask = new CommunityAsyncTask();
        communityAsyncTask.execute(path, jsonPath);
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    //瀑布流加载更多
    @Override
    public void onBottom() {

        pageno++;
        String path = "http://mhjk.1391.com/comic/getcommunitylist?e=" + (System.currentTimeMillis() / 1000);

        String jsonPath = "{\"userid\":\"\",\"pagesize\":\"10\",\"sort\":\"5\",\"isgood\":\"1\",\"pageno\":" + pageno + ",\"communityid\":\"0\"}";

        CommunityAsyncTask communityAsyncTask = new CommunityAsyncTask();
        communityAsyncTask.execute(path, jsonPath);
    }

    @Override
    public void itemOnClick(Info info, int position) {

    }

    class CommunityAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String ret = null;
            String path = params[0];
            String jsonPath = params[1];
            if (path != null && jsonPath != null) {
                String jsonStr = JsonUtils.doPostRequest(path, jsonPath);
                ret = JsonUtils.stringToRegex(jsonStr);
            }
            Log.d("CommunityFragment","ret:"+ret);
            return ret;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                try {
                    if (infos == null) {
                        infos = new LinkedList<>();
                    }
                    JSONObject jsonObject = new JSONObject(s);
                    JSONArray infoArr = jsonObject.getJSONArray("info");

                    for (int i = 0; i < infoArr.length(); i++) {
                        Info info = new Info();
                        JSONObject object = infoArr.getJSONObject(i);
                        info.jsonParse(object);
                        infos.add(info);
                    }
                    if (infos != null) {
                        for (int i = 0; i < infos.size(); i++) {
                            Info info = infos.get(i);
                            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_community_pubulu, null);
                            ImageView imageView = (ImageView) view.findViewById(R.id.item_community_network_image);
                            TextView textView = (TextView) view.findViewById(R.id.item_community_text);

                            if (info != null) {
                                String title = info.getTitle();
                                textView.setText(title);

                                List<Picurl> picurls = info.getPicurls();
                                Picurl picurl = picurls.get(0);
                                String smallpicture = picurl.getSmallpicture();
                                xUtilsImageLoader.display(imageView,smallpicture);
                                puBuLu.addPuBuLuView(view,infos,i);
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
